/*
 * ImportSources module for Netbeans.
 *
 * Copyright (c) 2007, Rogue
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * For queries or bugs, you may contact me via e-mail: roguexz [at] gmail [dot] com
 */
package rogue.netbeans.module.importsources.ui;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JComboBox;
import org.netbeans.api.java.project.JavaProjectConstants;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectUtils;
import org.netbeans.api.project.SourceGroup;
import org.netbeans.modules.web.api.webmodule.WebProjectConstants;


/**
 * UI representation of the WizardPanel3 class.
 *
 * @author Rogue
 */
public final class WizardPanel3UI extends javax.swing.JPanel {
    
    /** Creates new form WizardPanel3UI */
    public WizardPanel3UI() {
        initComponents();
        projectSelectionCB.removeAllItems();
        sourceRootSelectionCB.removeAllItems();
    }
    
    public void setSourceRoots(String[] roots) {
        sourceRootSelectionCB = new JComboBox(roots);
        sourceRootSelectionCB.setSelectedIndex(0);
    }
    
    public Project getSelectedProject() {
        if(projects!=null) {
            return projects[projectSelectionCB.getSelectedIndex()];
        }
        return null;
    }
    
    public SourceGroup getSelectedSourceGroup() {
        if(projects!=null && !sourceGroupList.isEmpty()) {
            return sourceGroupList.get(sourceRootSelectionCB.getSelectedIndex());
        }
        return null;
    }
    
    public String getName() {
        return "Select the source root.";
    }
    
    public void setProjects(Project[] projects) {
        this.projects = projects;
        for(int i=0; i<projects.length; i++) {
            projectSelectionCB.addItem(ProjectUtils.getInformation(projects[i]).getDisplayName());
        }
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        sourceRootSelectionCB = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        projectSelectionCB = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();

        jLabel1.setText("Select the source root to which you would like to copy the files");

        sourceRootSelectionCB.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel2.setText("Select the project to which you would like to import the sources to.");

        projectSelectionCB.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        projectSelectionCB.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                projectSelectionCBItemStateChanged(evt);
            }
        });

        jLabel3.setText("<html><b><u>Note:</u><br>Import sources action cannot be undone!!!<br>If you are sure about your selections, click on finish.\n</b></html>");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel2)
                    .add(projectSelectionCB, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel1)
                    .add(sourceRootSelectionCB, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel3))
                .addContainerGap(92, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel2)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(projectSelectionCB, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(sourceRootSelectionCB, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 146, Short.MAX_VALUE)
                .add(jLabel3)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    
    private void projectSelectionCBItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_projectSelectionCBItemStateChanged
        int idx = projectSelectionCB.getSelectedIndex();
        if(projects!=null) {
            sourceRootSelectionCB.removeAllItems();
            sourceGroupList.clear();
            
            SourceGroup[] g = ProjectUtils.getSources(projects[idx]).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
            for(int i=0; i<g.length; i++) {
                sourceRootSelectionCB.addItem(g[i].getDisplayName());
                sourceGroupList.add(g[i]);
            }
            g = ProjectUtils.getSources(projects[idx]).getSourceGroups(WebProjectConstants.TYPE_DOC_ROOT);
            for(int i=0; i<g.length; i++) {
                sourceRootSelectionCB.addItem(g[i].getDisplayName());
                sourceGroupList.add(g[i]);
            }
        }
    }//GEN-LAST:event_projectSelectionCBItemStateChanged
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JComboBox projectSelectionCB;
    private javax.swing.JComboBox sourceRootSelectionCB;
    // End of variables declaration//GEN-END:variables
    private Project[] projects = null;
    private List<SourceGroup> sourceGroupList = new ArrayList<SourceGroup>();
}
